/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.MedicoDAO;
import org.trandio.dao.MedicoInterface;
import org.trandio.dao.UtilidadesDAO;
import org.trandio.dao.UtilidadesInterface;
import org.trandio.hb.TbFechanoagenda;
import org.trandio.model.TurnoVO;
import org.trandio.util.Utiles;

/**
 *
 * @author root
 */
public class AgendaServlet extends HttpServlet {
   
    UtilidadesInterface utilidadesInterface;
    MedicoInterface medicoInterface;
    private List<TurnoVO> listTurno;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            medicoInterface = new MedicoDAO();
            utilidadesInterface  = new UtilidadesDAO();
            switch(Integer.parseInt(request.getParameter("op"))){
                case 1:

                    int sede = Integer.parseInt(request.getParameter("sede"));
                    int medico = Integer.parseInt(request.getParameter("medico"));

                    SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd/MM/yyyy");
                    String cadFecha = request.getParameter("fecha_ag");
                    String cadFechaag1 = request.getParameter("fecha_ag1");

                    Date fecha = null;
                    Date fecha1 = null;
                    try {
                        fecha = formatoDelTexto.parse(cadFecha);
                        fecha1 = formatoDelTexto.parse(cadFechaag1);
                    } catch (ParseException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                                      

                    String hinicio = request.getParameter("hinicio");
                    String hfin = request.getParameter("hfin");
                    int tiempo = Integer.parseInt(request.getParameter("tiempo"));
                    int turnos = Integer.parseInt(request.getParameter("turnos"));
                    String listasem = request.getParameter("listasem");
                    String listafes = request.getParameter("listafes");
                    
                    try{
                    
                    
                        medicoInterface.crearTurno(medico, fecha, fecha1, hinicio, hfin, tiempo, turnos, sede, listasem,listafes);
                    } catch (SQLException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;


                case 2:
                    int medicoSede = Integer.parseInt(request.getParameter("sede"));
                    int medicoTurno = Integer.parseInt(request.getParameter("medico"));
                    
                    try {
                        listTurno = medicoInterface.consultarTurnos(medicoTurno,medicoSede);
                    } catch (SQLException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    Iterator tturno = listTurno.iterator();


                    out.println("<table width='100%' id='tblturnos' style='border:1px solid gray' class='extdt-caption'>");
                    out.println("<tr class='ui-widget-header'><td colspan='6'>Turnos</td></tr>");
                    out.println("<tr class='ui-widget-header'><td>Inicio</td><td>Final</td><td>Tiempo</td><td>Turnos</td><td>Acci&oacute;n</td></tr>");
                    while (tturno.hasNext()) {
                        TurnoVO b = (TurnoVO) tturno.next();

                        out.println("<tr><td>" + b.getFecha() + " " + b.getHinicio() + "</td><td>"+b.getFechaf()+" " +b.getHfinal() + "</td><td>" + b.getTiempo() + "</td><td>" + b.getTurnos() + "</td>");
                        out.println("<td><img style='cursor:pointer' src='../common/img/del1.png' onclick='quitarTurno(" + b.getId() + ")' /></td></tr>");
                    }
                    out.println("</table>");
                    break;


                case 3:
                        int idturno = Integer.parseInt(request.getParameter("id"));
                        try {
                            medicoInterface.quitarTurno(idturno);
                        } catch (SQLException ex) {
                            Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NamingException ex) {
                            Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                        }
                break;


                case 4:
                    java.util.Date fechaAnterior = null,
                    fechaPosterior = null;


                    SimpleDateFormat formatoDelT = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                    //String cadFecha1 = "09/09/2010 08:30:00";
                    //String cadFecha2 = "09/09/2010 13:30:00";

                    String cadFecha1 = request.getParameter("fecha_ini");
                    String cadFecha2 = request.getParameter("fecha_fin");


                    try {


                        fechaAnterior = formatoDelT.parse(cadFecha1);
                        fechaPosterior = formatoDelT.parse(cadFecha2);

                        float diferencia = ((fechaPosterior.getTime() - fechaAnterior.getTime())/1000);
                        

                        out.println(diferencia);


                    } catch (ParseException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;


                    case 5:
                    int sed = Integer.parseInt(request.getParameter("sede"));
                    int med = Integer.parseInt(request.getParameter("medico"));


                    SimpleDateFormat fTexto = new SimpleDateFormat("dd/MM/yyyy");
                    String cadFechaVal = request.getParameter("fecha_ag");
                    String cadFechaag1Val = request.getParameter("fecha_ag1");

                    Date fechaVal = null;
                    Date fecha1Val = null;
                    try {
                        fechaVal = fTexto.parse(cadFechaVal);
                        fecha1Val = fTexto.parse(cadFechaag1Val);
                    } catch (ParseException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    String hinicioVal = request.getParameter("hinicio");
                    String hfinVal = request.getParameter("hfin");


                    try {
                        boolean resp = medicoInterface.validarTurno(med, fechaVal, fecha1Val, hinicioVal, hfinVal,sed);

                        if(resp==true){
                            out.println(1);
                        }else{
                            out.println(0);
                        }

                        
                        
                    } catch (SQLException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                        
                        
                    case 6:
                        int idtur = Integer.parseInt(request.getParameter("id"));
                        try {
                            out.println(medicoInterface.validarCita(idtur));
                        } catch (SQLException ex) {
                            Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NamingException ex) {
                            Logger.getLogger(AgendaServlet.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        
                    break;        
                        
                        


                    }

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
